package cc.comac.mom.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import cc.comac.mom.config.OSSService;
import cc.comac.mom.entity.EquipRepairDocEntity;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import cc.comac.mom.api.EquipRepairDocService;
import cc.comac.mom.bean.EquipRepair;
import cc.comac.mom.bean.EquipRepairDoc;
import cc.comac.mom.util.ResultBean;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value ="/EquipRepairDoc")
public class EquipRepairDocController {
	@Autowired
	private OSSService oSSService;
	@Autowired
	private EquipRepairDocService equipRepairDocService;
	
	@ApiOperation("批量插入维修文档")
	@RequestMapping("/insertBatch")
	public ResultBean insertBatch(@RequestBody List<EquipRepairDoc> list) {
		return equipRepairDocService.insertBatch(list);
	}

	@ApiOperation("删除维修文档")
	@RequestMapping("/delete")
	public ResultBean delete(@RequestBody EquipRepairDoc obj) {
		return equipRepairDocService.delete(obj);
	}
	
	@ApiOperation("查询维修文档分页")
	@RequestMapping("/selectByCondition")
	public ResultBean selectByCondition(@RequestBody EquipRepairDoc obj) {
		return equipRepairDocService.selectByCondition(obj);
	}
	
	@ApiOperation("查询维修知识库维修文档分页")
	@RequestMapping("/getRepqirRepositoryDocInfo")
	public ResultBean getRepqirRepositoryDocInfo(@RequestBody EquipRepair obj) {
		return equipRepairDocService.getRepqirRepositoryDocInfo(obj);
	}

	@GetMapping(value = "/previewPdf")
	public ResponseEntity<Object> previewPdf(HttpServletRequest request, HttpServletResponse response, @RequestParam long id)  {
		EquipRepairDocEntity selectByDocumentId = equipRepairDocService
				.selectByDocumentId(id);
		try {
			// 以流的形式下载文件。
			InputStream fis=oSSService.getFileByPathFromOss(selectByDocumentId.getDoucumentRealPath());

			ByteArrayResource bar = new ByteArrayResource(IOUtils.toByteArray(fis));

			return ResponseEntity.ok()
					.header("Content-Disposition", "inline;filename=" +selectByDocumentId.getDocumentName() + "")
					.contentType(MediaType.APPLICATION_PDF)
					.contentLength(bar.contentLength())
					.body(bar);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@GetMapping("/download/{documentId}")
	public ResultBean download(@PathVariable("documentId") Long documentId, HttpServletResponse response) {
		ResultBean resultBean = new ResultBean(true);
		EquipRepairDocEntity selectByDocumentId = equipRepairDocService
				.selectByDocumentId(documentId);

		try {
			oSSService.downFileFromOSS(response, selectByDocumentId.getDoucumentRealPath(),selectByDocumentId.getDocumentName());
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return resultBean;
	}
}
